package sc.tool.sqlgen.branchinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sc.prototype.common.ConsoleUtil;

public class DataService implements ConsoleUtil {

    private static Map<String, Area> areaMap;
    private static Map<String, Bank> bankMap;

    //1.和数据源文件进行比较
    //2.显示差异
    //3.导出差异sql

    public static List<BranchInfo> loadBranchInfoFromDb() {
        DBUtil dbUtil = new DBUtil();
        List<Object[]> result = dbUtil.query("select * from tm_branch_info", "ID", "BRANCH_ID",
                "BRANCH_NAME", "ALIAS_NAMES", "UNIFIED_BANK_ID", "BANK_CODE", "PROV_ID", "CITY_ID");

        List<BranchInfo> dbDataList = new ArrayList<BranchInfo>();
        for (Object[] row : result) {
            BranchInfo dbData = new BranchInfo();
            dbData.setId(new Long(row[0].toString()));
            dbData.setBranchId(row[1].toString());
            dbData.setBranchName(row[2].toString());
            dbData.setAliasNames(row[3].toString());
            dbData.setUnifiedBankId(row[4].toString());
            dbData.setBankCode(row[5].toString());
            dbData.setProvId(row[6].toString());
            dbData.setCityId(row[7].toString());
            dbDataList.add(dbData);
        }
        return dbDataList;
    }

    public static final String AREA_FILE = "D:/WeibopayMemo/出款服务FOS/area.xlsx";
    public static final String BANK_FILE = "D:/WeibopayMemo/出款服务FOS/bank.xlsx";

    /**
     * 加载地区信息
     * @return
     */
    public static Map<String, Area> loadAreas() {
        if (areaMap != null) {
            return areaMap;
        }
        if (areaMap == null) {
            areaMap = new HashMap<String, Area>();
        }
        List<String[]> areaData = XlsxUtil.loadItems(AREA_FILE, "area");
        for (int i = 1; i < areaData.size(); i++) {
            Area area = new Area();
            area.setAreaCode(areaData.get(i)[0]);
            area.setAreaName(areaData.get(i)[1]);
            area.setCityId(areaData.get(i)[4]);
            area.setProvId(areaData.get(i)[5]);
            areaMap.put(area.getAreaCode(), area);
        }
        Console.info("Area loaded, size[{}]", areaData.size());
        return areaMap;
    }

    public static Map<String, Bank> loadBanks() {
        if (bankMap != null) {
            return bankMap;
        }
        if (bankMap == null) {
            bankMap = new HashMap<String, Bank>();
        }
        List<String[]> bankData = XlsxUtil.loadItems(BANK_FILE, "bank");
        for (int i = 1; i < bankData.size(); i++) {
            Bank bank = new Bank();
            bank.setBankName(bankData.get(i)[0]);
            bank.setBankCode(bankData.get(i)[1]);
            bank.setUnifiedBankId(bankData.get(i)[2]);
            bankMap.put(bank.getUnifiedBankId(), bank);
        }
        Console.info("Bank loaded, size[{}]", bankData.size());
        return bankMap;
    }

    public static Area getAreaByBranchId(String branchId) {
        return loadAreas().get(getAreaCode(branchId));
    }

    public static Bank getBankByBranchId(String branchId) {
        return loadBanks().get(getUnifiedBankId(branchId));
    }

    public static String getUnifiedBankId(String branchId) {
        return branchId.substring(0, 3);
    }

    public static String getAreaCode(String branchId) {
        return branchId.substring(3, 7);
    }

    public static void main(String[] args) {
        /*  String branchId = "313515600280";
          System.out.println("branchId=" + branchId);
          System.out.println("unifiedBankId=" + getUnifiedBankId(branchId));
          System.out.println("areaCode=" + getAreaCode(branchId));*/

        //        System.out.println(loadBranchInfoFromDb());
        //        System.out.println(loadAreas());
        System.out.println(loadBanks());
    }

}
